package com.ilikesoup.instant.monitor.sample.trigger;

import com.ilikesoup.instant.monitor.core.ChangeEventTrigger;
import com.ilikesoup.instant.monitor.core.Invocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class MethodInvokeTrigger implements ChangeEventTrigger {
    @Override
    public Object copyOldValue(Invocation invocation) {
        return null;
    }

    @Override
    public void fireChange(Invocation invocation, Object oldValue, PropertyChangeSupport propertyChangeSupport) {
        propertyChangeSupport.firePropertyChange(invocation.getMethodName(), invocation.getSerializedArgs(), invocation.getSerializedRetVal());
    }

    public static class MethodInvokeListener implements PropertyChangeListener {
        private static final Logger logger = LoggerFactory.getLogger(MethodInvokeListener.class);
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            logger.debug("方法调用监听，对象信息：{}，调用信息：[method:{}, args:{}, return:{}]",
                    evt.getSource(), evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
        }
    }

}
